View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladimir Radisic 5 * @Version 2.0.1 6 */ 7 8 package org.webdocwf.util.smime.test; 9 10 11 import javax.mail.Transport; 12 import org.webdocwf.util.smime.smime.SignedAndEnvelopedSMIME; 13 import org.webdocwf.util.smime.exception.SMIMEException; 14 import java.io.File; 15 import java.io.ByteArrayInputStream; 16 import java.io.InputStream; 17 18 19 /*** 20 * Tests enveloping and signing process. Enveloped and signed 21 * text/html message with attachments can be sent by this test. This example 22 * is test for composing of email message content by html code which is generated 23 * by application, and adding resources assocated in html code from the 24 * application, and from the file system. Also, attachments are added to message 25 * from the application, or from file system. To get help for this example type: 26 * "java org.webdocwf.util.smime.test.TestEncSigGeneratedHtml" in command line. 27 * It is assumed that smime200tests.jar is in your classpath.<BR> 28 * <BR> 29 * Parameters passed to example are:<BR> 30 * <mailHost> <mailAddress> <cerFileName> <algorithmName> 31 * <digestAlgorithm> <includingCert> <includingSignAttrib> 32 * <pfxFileName> [<attachment>] <BR> 33 * <BR> 34 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR> 35 * <includingCert> could be: true/false<BR> 36 * <includingSignAttrib> could be: true/false<BR> 37 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR> 38 * <BR> 39 * Note that for this example passwords for .pfx or .p12 files are fixed to 40 * "sea1". All .pfx files or .p12 files provided with this example have this 41 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu". 42 * You should change this values in source code of TestEncSigGeneratedHtml.java 43 * in order to use them with other .pfx or .p12 files and corresponding "FROM" 44 * addresses and passwords. 45 */ 46 public class TestEncSigGeneratedHtml { 47 48 public static void main(String[] args) { 49 50 String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß"; 51 String from = "sender@seateam.co.yu"; 52 String password = "sea1"; 53 54 if (args.length < 8) { 55 System.err.println( 56 System.getProperty("line.separator") + 57 "Usage of TestEncSigGeneratedHtml: " + 58 System.getProperty("line.separator") + 59 "java TestEncSigGeneratedHtml <mailHost> <mailAddress> <cerFileName> " + 60 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 61 "<pfxFileName> [<attachment>]" + 62 System.getProperty("line.separator") + 63 System.getProperty("line.separator") + 64 "Examples:" + 65 System.getProperty("line.separator") + 66 "java TestEncSigGeneratedHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 67 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 68 System.getProperty("line.separator") + 69 "java TestEncSigGeneratedHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 70 "DES MD5_WITH_RSA true true sender512.pfx .//test//Zip8Test.zip"); 71 72 System.exit(-1); 73 } 74 75 String smtpHost = args[0]; 76 String addressTO = args[1]; 77 String cerFileName = args[2]; 78 String algorithmName = args[3]; 79 String digestAlgorithm = args[4]; 80 81 boolean includingCert = true; 82 83 if (args[5].equals("true")) 84 includingCert = true; 85 else 86 includingCert = false; 87 88 boolean includingSignAttrib = true; 89 90 if (args[6].equals("true")) 91 includingSignAttrib = true; 92 else 93 includingSignAttrib = false; 94 95 String pfxfileName = args[7]; 96 97 String fileName = null; 98 99 if (args.length > 8) 100 fileName = args[8]; 101 102 String addressCC = "recipient@seateam.co.yu"; 103 String addressBCC = "recipient@seateam.co.yu"; 104 105 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 106 args[6] + " " + args[7] + " " + subject; 107 108 SignedAndEnvelopedSMIME ess = null; 109 110 try { 111 ExampleGenerator generator = new ExampleGenerator(); 112 113 // Address for resource green.gif which will be obtained by program generation 114 // from instances of the ExampleGenerator class as InputSteam must be added to 115 // html code as virtual. For more information refer to setContent method of 116 // SignedSMIME class. 117 String green = "*****000generated.gif"; 118 119 // Address for resource red.gif class will be defined as absolute address to 120 // location on the file system. 121 File redFile = new File("./test/pictures/red.gif"); 122 String red = redFile.getAbsoluteFile().getCanonicalPath(); 123 124 // Address for resource blue.gif class will be defined as file type of url 125 // address. 126 File blueFile = new File("./test/pictures/blue.gif"); 127 String blue = "file:///" + blueFile.getAbsoluteFile().getCanonicalPath(); 128 129 blue = blue.replace('//', '/'); 130 131 // Addresses for resources orange.jpg and yellow.jpg are left relative. 132 // Default values of resources in ExampleGenerator are relative, as it can be 133 // seen in the original file of html example HtmlTest.html. To resolve this 134 // relative adresses common directory path must be obtained from the root. 135 // For more information refer to setContent method of 136 // SignedSMIME class. 137 String commonPath = new File("./test/HtmlTest.html").getParent(); 138 139 generator.setResourceInExampleHtml("GREEN", green); 140 generator.setResourceInExampleHtml("RED", red); 141 generator.setResourceInExampleHtml("BLUE", blue); 142 143 // Simulation of resources (green.gif) obtained by program. 144 InputStream[] in = {generator.getGifImage()}; 145 146 // Generation of signed smime object 147 ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject); 148 // es.setContent( generator.getHtmlStream(), "text/html", commonPath, in ); 149 ess.setContent(generator.getHtmlString(), "text/html", commonPath, in); 150 151 // Attachments can be added to message from program (using Input stream). In 152 // the following line zip file will be generated and added as attachment. You must 153 // define some file names in order to give appropriate mime-type to your message. 154 ess.addAttachment(generator.getZipStream(), "test.zip"); 155 156 // File can also be added from file system 157 File attachment2 = new File("./test/AdobeAcrobatTest.pdf"); 158 159 ess.addAttachment(attachment2); 160 File attachment3 = new File("./test/Word2000Test.doc"); 161 162 ess.addAttachment(attachment3); 163 164 if (fileName != null) { 165 ess.addAttachment(fileName); // optional - use this if send attachment 166 } 167 168 ess.setReply(from); // optional 169 170 ess.addRecipient(addressTO, "TO", cerFileName); // mandatory 171 // ess.addRecipient(addressCC, "CC", cerFileName); // optional 172 // ess.addRecipient(addressBCC, "BCC", cerFileName); // optional 173 174 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional 175 ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional 176 ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional 177 178 ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 179 180 if (algorithmName.equals("RC240")) { 181 System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... "); 182 ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used 183 // ess.enveloping("RC2_CBC", 40, "ENCRYPT_FIRST"); 184 } else if (algorithmName.equals("RC264")) { 185 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... "); 186 ess.signingAndEnveloping("RC2_CBC", 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm 187 } else if (algorithmName.equals("RC2128")) { 188 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... "); 189 ess.signingAndEnveloping("RC2_CBC", 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm 190 } else if (algorithmName.equals("DES")) { 191 System.out.println("Creating enveloped and signed message with DES algorithm... "); 192 ess.signingAndEnveloping("DES", 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm 193 } else if (algorithmName.equals("3DES")) { 194 System.out.println("Creating enveloped and signed message with 3DES algorithm... "); 195 ess.signingAndEnveloping("DES_EDE3_CBC", 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm 196 } 197 198 System.out.print("Sending enveloped and signed message ... "); 199 ess.send(); // instead of this next line could be used 200 // Transport.send(ess.getSignedMessage()); 201 System.out.println("done."); 202 203 } catch (Exception e) { 204 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging 205 if (e instanceof SMIMEException) { 206 // ((SMIMEException)e).loggingErrors(null); //logging 207 ((SMIMEException) e).displayErrors(null); 208 } else 209 System.out.println(e.getMessage()); 210 } 211 } 212 }

This page was automatically generated by Maven